package com.lhj.library.domain.service.impl;

import com.alibaba.fastjson.JSON;
import com.lhj.library.common.entity.PageResult;
import com.lhj.library.domain.convert.RecordBoConverter;
import com.lhj.library.domain.entity.RecordBO;
import com.lhj.library.domain.service.RecordDomainService;
import com.lhj.library.infra.basic.entity.Record;
import com.lhj.library.infra.basic.service.RecordService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
@Slf4j
public class RecordDomainServiceImpl implements RecordDomainService {

    @Resource
    private RecordService recordService;


    @Override
    public PageResult<RecordBO> selectByPage(RecordBO recordBO) {
        if (log.isInfoEnabled()) {
            log.info("RecordController.selectByPage.bo:{}", JSON.toJSONString(recordBO));
        }
        PageResult<RecordBO> boPageResult = new PageResult<>();
        boPageResult.setPageNo(recordBO.getPageNo());
        boPageResult.setPageSize(recordBO.getPageSize());
        int start = (recordBO.getPageNo() - 1) * recordBO.getPageSize();
        Record record = RecordBoConverter.INSTANCE.convertBoToRecord(recordBO);
        int count = recordService.count(record);
        if (count == 0) {
            return boPageResult;
        }
        List<Record> recordList = recordService.selectByPage(record, start, recordBO.getPageSize());
        List<RecordBO> recordBOS = RecordBoConverter.INSTANCE.convertRecordToBoList(recordList);
        boPageResult.setRecords(recordBOS);
        boPageResult.setResult(recordBOS);
        return boPageResult;
    }
}
